<!--
 * @Descripttion: 
 * @LastEditors: xzh
 * @LastEditTime: 2024-06-10 14:34:40
-->
<template>
  <DileDrawer
    v-model="recordDrawerParam.visible"
    :loading="recordDrawerParam.loading"
    size="80%"
    @sure="hanldeSure"
  >
    <template #header>
      <span>{{
        $t(`table.btnArr.${!recordDrawerParam.id ? "add" : "edit"}`)
      }}</span>
    </template>
    <template #default>
      <!-- 设备维修表单 -->
      <RepairRecordForm ref="repairRecordFormRef" />
    </template>
  </DileDrawer>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick, toRefs } from "vue";
import { object_Inf } from "@/utils/typings/index.type";
import RepairRecordForm from "../index.vue";
import DileDrawer from "@/components/dile-drawer/index.vue";
const emit = defineEmits(["refresh"]);
/**
 * @Author: xzh
 * @Descripttion: 设备维修表单实例
 * @Param:
 */
let repairRecordFormRef = ref<any>(null);
/**
 * @Author: xzh
 * @Descripttion: 记录抽屉参数
 * @Param:
 */
let recordDrawerParam = reactive<object_Inf>({
  visible: false,
  loading: false,
  id: null,
});

/**
 * @Author: xzh
 * @Descripttion:初始化加载
 * @Param:
 * @param {*} id 保养计划主键
 */
let init = async (id: number, defaultData: object_Inf = {}) => {
  recordDrawerParam.visible = true;
  await nextTick();
  try {
    Object.assign(recordDrawerParam, {
      loading: true,
      id,
    });
    //加载表单数据
    await repairRecordFormRef.value.initFormData(id, defaultData);
  } finally {
    recordDrawerParam.loading = false;
  }
};
/**
 * @Author: xzh
 * @Descripttion: 保存事件
 * @Param:
 */
let hanldeSure = async () => {
  let { loading, visible } = toRefs(recordDrawerParam);
  if (!!loading.value) {
    return false;
  }
  try {
    loading.value = true;
    let recordInfo: object_Inf | boolean =
      await repairRecordFormRef.value.hanldeSaveForm();
    //保存表单数据
    if (recordInfo == false) {
      return false;
    }
    //触发刷新回调
    emit("refresh", recordInfo);
    //关闭弹框
    visible.value = false;
  } finally {
    loading.value = false;
  }
};
defineExpose({
  init,
});
</script>
<style lang="scss" scoped></style>
